<template>
  <div>
    <ComplexTable
      ref="brandFromTable"
      :query-model="queryModel"
      :table-column-is-show="tableColumnIsShow"
      :create-permission="['pms:brand:create']"
      @onSearch="search"
      @onAddPage="addPage"
    >
      <template v-slot:search_from_input>
        <el-form-item label="品牌名称" prop="name">
          <el-input
            v-model="queryModel.name"
            placeholder="品牌名称"
            clearable
          />
        </el-form-item>
      </template>
      <template v-slot:table_column>
        <el-table-column
          type="selection"
          width="40"
        />
        <el-table-column
          v-if="tableColumnIsShow.logo.isShow"
          prop="logo"
          label="品牌logo"
          align="center"
          header-align="center"
        >
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.logo"
            />
          </template>
        </el-table-column>
        <el-table-column
          v-if="tableColumnIsShow.name.isShow"
          prop="name"
          label="品牌名称"
          align="center"
          header-align="center"
        />
        <el-table-column
          v-if="tableColumnIsShow.firstLetter.isShow"
          prop="firstLetter"
          label="检索首字母"
          align="center"
          header-align="center"
        />
        <el-table-column
          v-if="tableColumnIsShow.status.isShow"
          prop="status"
          label="状态"
          align="center"
          header-align="center"
        >
          <template slot-scope="scope">
            {{ getSystemDictionaries("USESTATUS",scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="tableColumnIsShow.sort.isShow"
          prop="sort"
          label="排序"
          align="center"
          header-align="center"
        />
        <el-table-column
          label="操作"
          width="200"
          align="center"
          header-align="center"
        >
          <template slot-scope="scope">
            <TableBtn :parameter="scope.row.id" :edit-permission="['pms:brand:update' ]" :del-permission="['pms:brand:delete' ]" @onDel="del" @onEditPage="editPage" />
          </template>
        </el-table-column>
      </template>
    </ComplexTable>

    <!--弹出框-->
    <MyEditDialog :dialog-visible="dialogVisible" :title="title" width="950px" @onDialogSave="save" @onGoBack="dialogVisible=false">
      <template v-slot:dialog_content>
        <EditForm v-if="dialogVisible" ref="EditForm" :form-model="editFormModel" :validate="formValidate" />
      </template>
    </MyEditDialog>
  </div>
</template>

<script>
import TableBtn from '@/components/TableBtn'
import ComplexTable from '@/components/ComplexTable'
import { pageBrandREQ, getBrandByIdREQ, updateBrandREQ, createBrandREQ, deleteBrandREQ } from '@/api/product/brand'
import MyEditDialog from '@/components/MyEditDialog'
import { mapGetters } from 'vuex'
import EditForm from './EditForm'
import deepcopy from 'deepcopy'
import { addValidate, editValidate } from './validate'
export default {
  name: 'RoleIndex',
  components: { ComplexTable, TableBtn, MyEditDialog, EditForm },
  data() {
    return {
      queryModel: {
        name: ''
      },
      tableColumnIsShow: {
        logo: { label: '品牌logo', isShow: true },
        name: { label: '品牌名称', isShow: true },
        status: { label: '状态', isShow: true },
        firstLetter: { label: '检索首字母', isShow: true },
        sort: { label: '排序', isShow: true }
      },
      page: null,
      title: '',
      dialogVisible: false,
      defaultEditForm: {
        name: '',
        logo: '',
        firstLetter: '',
        sort: 10,
        description: '',
        status: 'NORMAL'
      },
      editFormModel: {
      },
      formValidate: null
    }
  },
  computed: {
    ...mapGetters({ 'getSystemDictionaries': 'dictionaries/getSystemDictionaries' })
  },
  mounted() {

  },
  methods: {
    search(queryModel = {}) {
      pageBrandREQ(queryModel).then((data) => {
        // 双重认证
        if (this.$refs['brandFromTable']) { this.$refs['brandFromTable'].setTableAndPage(data) }
      })
    },
    del(id) {
      if (id) {
        deleteBrandREQ(id).then(v => {
          this.search({ current: 1 })
        })
      }
    },
    editPage(id) {
      if (id) {
        this.title = '编辑品牌信息'
        getBrandByIdREQ(id).then(data => {
          this.editFormModel = deepcopy(data)
          this.dialogVisible = true
          this.formValidate = editValidate
        })
      }
    },
    save() {
      this.$refs['EditForm'].$refs['EditForm'].validate((valid) => {
        if (valid) {
          if (this.editFormModel.id) {
            updateBrandREQ(this.editFormModel).then(v => {
              this.$refs['brandFromTable'].onSearch()
              this.dialogVisible = false
            })
          } else {
            createBrandREQ(this.editFormModel).then(v => {
              this.$refs['brandFromTable'].onSearch()
              this.dialogVisible = false
            })
          }
        }
      })
    },
    addPage() {
      this.title = '新增品牌信息'
      this.editFormModel = deepcopy(this.defaultEditForm)
      this.dialogVisible = true
      this.formValidate = addValidate
    }
  }

}

</script>
<style lang='scss' scoped>
</style>
